var par = document.querySelector('.am-pagination');
var sum = document.querySelector('.sum');
$('.se1').hide();
$('.se2').hide();
var url = '';
if (sessionStorage.getItem('activeid') != null) {
	$('#inav li')[sessionStorage.getItem('activeid')].className = 'active';
	selectAll(sessionStorage.getItem('activeid'));
	// if (num == 3) {
	// 	url = 'http://localhost:3000/admin/newList';
	// }
	// if (num == 2) {
	// 	url = 'http://localhost:3000/admin/hotList';
	// }
	// if (num == 1) {
	// 	url = 'http://localhost:3000/admin/goodList';
	// }
}
$('#inav li').click(function () {
	$(this).addClass('active').siblings().removeClass('active');
});
var pa = 1;
var max = 0;

function selectAll(num) {
	$('.se1').hide();
	$('.se2').hide();
	sessionStorage.removeItem('moretoday');
	sessionStorage.removeItem('activeid');
	sessionStorage.removeItem('typeid');
	sessionStorage.removeItem('goodsname');
	// alert(num);
	if (num == 3) {
		pa = 1;
		url = 'http://localhost:3000/admin/newList';
		getGoodsList(pa, 8, 'http://localhost:3000/admin/newList');
	}
	if (num == 2) {
		pa = 1;
		url = 'http://localhost:3000/admin/hotList';
		getGoodsList(pa, 8, 'http://localhost:3000/admin/hotList');
	}
	if (num == 1) {
		pa = 1;
		url = 'http://localhost:3000/admin/goodList';
		getGoodsList(pa, 8, 'http://localhost:3000/admin/goodList', 1);
	}
}

function getGoodsList(page, size, url, type) {
	$.ajax({
		url: url,
		method: 'GET',
		async: false,
		data: { page: page, size: size, type: type },
		success: function (data) {
			$('.goodscont')[0].innerHTML = '';
			sum.innerHTML = '总共' + data.data.total + '条记录';
			max = data.data.pageMax;
			data.data.dataList.forEach(function (good, index) {
				$('.goodscont')[0].innerHTML += `
      <div class="goods-box animated wobble">
      <div class="goods">
        <a href="javascript:void(0)">
          <div class="img-box">
            <img src="http://localhost:3000/update/${good.cover}">
          </div>
        </a>
        <a href="javascript:void(0)" onclick="datai(${good.id})">
          <p class="goodsname">${good.name}</p>
        </a>
        <p class="goods-descript">${good.intro}</p>
        <p class="goods-spec">${good.spec}</p>
        <p class="goodsprices" style="display:block;">
          <span class="yj">￥<span class="yjcont">${good.price}</span></span>
        </p>
        <p class="addcart" data-id="${good.id}" style="display:none;">
          <i class="iconfont icon-gouwuche3"></i>加入购物车
        </p>
      </div>
    </div>
      `;
			});
			pageHelper(par, data.data.pageMax);
			init();
		}
	});
}
function now(page) {
	pa = page;
	getGoodsList(pa, 8, url, 1);
}

//下一页
function nextpa() {
	if (pa + 1 >= max) {
		getGoodsList(max, 8, url, 1);
		pa = max;
	} else {
		pa += 1;
		getGoodsList(pa, 8, url, 1);
	}
}
function befor() {
	console.log(pa);
	if (pa - 1 == 0) {
		getGoodsList(1, 8, url, 1);
		pa = 1;
	} else {
		pa -= 1;
		getGoodsList(pa, 8, url, 1);
	}
}

function init() {
	$('.goods-box').on('mouseenter', function () {
		$(this).addClass('active');
		$(this).find('.addcart').show();
		$(this).find('.reserve').show();
		$(this).find('.m_stop').show();
		$(this).find('.goodsprices').hide();
	});
	$('.goods-box').on('mouseleave', function () {
		$(this).removeClass('active');
		$(this).find('.addcart').hide();
		$(this).find('.reserve').hide();
		$(this).find('.m_stop').hide();
		$(this).find('.goodsprices').show();
	});
	$('.goods-box1').on('mouseenter', function () {
		$(this).addClass('active');
	});
	$('.goods-box1').on('mouseleave', function () {
		$(this).removeClass('active');
	});
	$('.goods-box2').on('mouseenter', function () {
		$(this).addClass('active');
		$(this).find('.addcart').show();
		$(this).find('.reserve').show();
		$(this).find('.goodsprices').hide();
	});
	$('.goods-box2').on('mouseleave', function () {
		$(this).removeClass('active');
		$(this).find('.addcart').hide();
		$(this).find('.reserve').hide();
		$(this).find('.goodsprices').show();
	});
}

//根据类目id查询
if (sessionStorage.getItem('typeid') != null) {
	sessionStorage.removeItem('goodsname');
	sessionStorage.removeItem('moretoday');
	getGoodsByTypeIds(1, sessionStorage.getItem('typeid'));
}

function getGoodsByTypeIds(page, typeid) {
	$.ajax({
		url: 'http://localhost:3000/admin/queryGoodsByType',
		method: 'GET',
		async: false,
		data: { page: page, size: 8, typeId: typeid },
		success: function (data) {
			console.log(data);
			$('.goodscont')[0].innerHTML = '';
			sum.innerHTML = '总共' + data.data.total + '条记录';
			max = data.data.pageMax;
			data.data.dataList.forEach(function (good, index) {
				$('.goodscont')[0].innerHTML += `
      <div class="goods-box animated wobble">
      <div class="goods">
        <aonclick="datai(${good.id})" href="javascript:void(0)">
          <div class="img-box">
            <img src="http://localhost:3000/update/${good.cover}">
          </div>
        </a>
        <a onclick="datai(${good.id})" href="javascript:void(0)">
          <p class="goodsname">${good.name}</p>
        </a>
        <p class="goods-descript">${good.intro}</p>
        <p class="goods-spec">${good.spec}</p>
        <p class="goodsprices" style="display:block;">
          <span class="yj">￥<span class="yjcont">${good.price}</span></span>
        </p>
        <p class="addcart" data-id="${good.id}" style="display:none;">
          <i class="iconfont icon-gouwuche3"></i>加入购物车
        </p>
      </div>
    </div>
      `;
			});
			par.innerHTML = `          <li>
      <a onclick="befortype()" href="javascript:void(0)">«</a>
    </li>`;
			for (var j = 0; j < data.data.pageMax; j++) {
				par.innerHTML += `
        <li>
        <a onclick="nowtype(${j + 1})" href="javascript:void(0)">${j + 1}</a>
      </li>
        `;
			}
			par.innerHTML += `<li>
      <a onclick="nextpatype()" href="javascript:void(0)">»</a>
    </li>`;
			init();
			$('.se1').show();
			$('.se2').show();
			$('.tyname')[0].innerHTML = sessionStorage.getItem('name');
			$('.se2').hide();
		}
	});
}

// function datai() {
// 	alert('5');
// }
function nowtype(page) {
	pa = page;
	getGoodsByTypeIds(pa, sessionStorage.getItem('typeid'));
}

//下一页
function nextpatype() {
	if (pa + 1 >= max) {
		getGoodsByTypeIds(max, sessionStorage.getItem('typeid'));
		pa = max;
	} else {
		pa += 1;
		getGoodsByTypeIds(pa, sessionStorage.getItem('typeid'));
	}
}
function befortype() {
	console.log(pa);
	if (pa == 1 && pa == '1') {
		getGoodsByTypeIds(1, sessionStorage.getItem('typeid'));
		pa = 1;
	} else {
		pa -= 1;
		getGoodsByTypeIds(pa, sessionStorage.getItem('typeid'));
	}
}
//更多推荐及热销
if (sessionStorage.getItem('moretoday') != null) {
	if (sessionStorage.getItem('moretoday') == 1) {
		getGoodsList(pa, 8, 'http://localhost:3000/admin/goodList', 1);
		url = 'http://localhost:3000/admin/goodList';
	} else {
		getGoodsList(pa, 8, 'http://localhost:3000/admin/hotList');
		url = 'http://localhost:3000/admin/hotList';
	}
}

//模糊查询
if (sessionStorage.getItem('goodsname') != null) {
	findGodosByName(1, sessionStorage.getItem('goodsname'));
}
function findGodosByName(page, name) {
	$.ajax({
		url: 'http://localhost:3000/admin/queryName',
		async: false,
		method: 'GET',
		data: {
			page: page,
			size: 8,
			name: name
		},
		success: function (data) {
			console.log(data.data.dataList.length == 0);
			if (data.data.total == 0) {
				$('.goodscont')[0].innerHTML = `
        <div id="searchnull">
        <img src="./static/index/img/searchnull.png">
        <p>很抱歉，没有找到相关产品~<br>换个关键词吧~</p>
      </div>     
        `;
				return;
			}
			$('.goodscont')[0].innerHTML = '';
			sum.innerHTML = '总共' + data.data.total + '条记录';
			max = data.data.pageMax;
			data.data.dataList.forEach(function (good, index) {
				$('.goodscont')[0].innerHTML += `
      <div class="goods-box animated wobble">
      <div class="goods">
        <a onclick="datai(${good.id})" href="javascript:void(0)">
          <div class="img-box">
            <img src="http://localhost:3000/update/${good.cover}">
          </div>
        </a>
        <a onclick="datai(${good.id})" href="javascript:void(0)">
          <p class="goodsname">${good.name}</p>
        </a>
        <p class="goods-descript">${good.intro}</p>
        <p class="goods-spec">${good.spec}</p>
        <p class="goodsprices" style="display:block;">
          <span class="yj">￥<span class="yjcont">${good.price}</span></span>
        </p>
        <p class="addcart" data-id="${good.id}" style="display:none;">
          <i class="iconfont icon-gouwuche3"></i>加入购物车
        </p>
      </div>
    </div>
      `;
			});
			par.innerHTML = `          <li>
      <a onclick="beforfind()" href="javascript:void(0)">«</a>
    </li>`;
			for (var j = 0; j < data.data.pageMax; j++) {
				par.innerHTML += `
        <li>
        <a onclick="findnow(${j + 1})" href="javascript:void(0)">${j + 1}</a>
      </li>
        `;
			}
			par.innerHTML += `<li>
      <a onclick="nextfind()" href="javascript:void(0)">»</a>
    </li>`;
			init();
			$('.se1').show();
			$('.se2').show();
			$('.godname').html(sessionStorage.getItem('goodsname'));
			$('.se1').hide();
		}
	});
}

function findnow(page) {
	pa = page;
	findGodosByName(pa, sessionStorage.getItem('goodsname'));
}

//下一页
function nextfind() {
	if (pa + 1 >= max) {
		findGodosByName(max, sessionStorage.getItem('goodsname'));
		pa = max;
	} else {
		pa += 1;
		findGodosByName(pa, sessionStorage.getItem('goodsname'));
	}
}
function beforfind() {
	if (pa == 1 && pa == '1') {
		findGodosByName(1, sessionStorage.getItem('goodsname'));
		pa = 1;
	} else {
		pa -= 1;
		findGodosByName(pa, sessionStorage.getItem('goodsname'));
	}
}
